import axios from "axios";
import qs from 'qs';
import { isPlainObject, storage } from '@/assets/utils';
import { Notify } from 'vant';

const http = axios.create({
    baseURL: '/api',
    timeout: 60000,
    transformRequest: data => {
        if (isPlainObject(data)) return qs.stringify(data);
        return data;
    }
});

const safeList = ['/user_info', '/user_update', '/store', '/store_remove', '/store_list'];
http.interceptors.request.use(config => {
    // 指定部分接口传递Token
    let token = storage.get('tk');
    if (token && safeList.includes(config.url)) {
        config.headers['authorzation'] = token;
    }
    return config;
});

http.interceptors.response.use(response => {
    return response.data;
}, reason => {
    Notify({
        type: 'danger',
        message: '当前网络繁忙,请稍后再试~'
    });
    return Promise.reject(reason);
});
export default http;